<template>
  <div class="page-group-order">
    <!-- 新版规范 -->
    <div class="group-list-mod">
      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form
          ref="test-form"
          :inline="true"
          class="search-form"
          :model="searchForm"
          label-width="auto"
          @submit.prevent
        >
          <div class="input-row">
            <el-form-item
              :label="$t('groups.eventName') + ':'"
              class="search-form-item"
            >
              <el-input
                v-model="searchForm.activityName"
                :placeholder="$t('groups.eventName')"
              />
            </el-form-item>
            <el-form-item>
              <div
                class="default-btn primary-btn"
                @click="onSearch(true)"
              >
                {{ $t('group.search') }}
              </div>
              <div
                class="default-btn"
                @click="clearSearch"
              >
                {{ $t('shop.resetMap') }}
              </div>
            </el-form-item>
          </div>
        </el-form>
      </div>
      <!-- 搜索栏end -->
      <!-- 表格主体 -->
      <div class="main-container">
        <!-- 表格 -->
        <div class="table-con group-team">
          <el-table
            :data="dataList"
            header-cell-class-name="table-header"
            row-class-name="table-row-low"
            style="width: 100%"
          >
            <el-table-column
              prop="activityName"
              :label="$t('groups.eventName')"
              width="240"
            >
              <template #default="scope">
                <div>
                  <span class="table-cell-text">{{ scope.row.activityName || '-' }}</span>
                </div>
              </template>
            </el-table-column>
            <!-- 商品信息 -->
            <el-table-column
              align="left"
              prop="prodName"
              :label="$t('product.prodInfo')"
              min-width="200"
            >
              <template #default="scope">
                <div class="table-cell-con">
                  <div class="table-cell-image">
                    <ImgShow :src="scope.row.pic" />
                  </div>
                  <span class="table-cell-text">{{ scope.row.prodName }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="startTime"
              :label="$t('group.groupTime')"
            >
              <template #default="scope">
                <div>
                  <span class="table-cell-text">{{
                    scope.row.startTime ? scope.row.startTime : $t('group.notOpenGroup')
                  }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="groupNumber"
              :label="$t('groups.numberOfParticipants')"
              min-width="110"
              sortable
            >
              <template #default="scope">
                <div>
                  <span>{{ scope.row.joinNum }}/{{ scope.row.groupNumber || '-' }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="totalPrice"
              :label="$t('group.totalOrderAmount')"
              min-width="120"
              sortable
            >
              <template #default="scope">
                <div>
                  <span>{{ scope.row.totalPrice }}</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="status"
              :label="$t('order.status')"
              width="100px"
            >
              <template #default="scope">
                <div class="tag-text">
                  {{
                    [
                      $t('group.waitGroupUnpay'),
                      $t('group.inAGroup'),
                      $t('group.succ'),
                      $t('group.failGroup')
                    ][scope.row.status]
                  }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              :label="$t('order.operation')"
            >
              <template #default="scope">
                <div
                  v-if="isAuth('group:team:info')"
                  class="default-btn text-btn marginLe"
                  @click="viewGroupOrderInfo(scope.row.groupTeamId)"
                >
                  {{ $t('groups.viewGroupOrders') }}
                </div>
                <div
                  class="default-btn text-btn marginLBtn"
                  @click="viewGroupActivityInfo(scope.row.groupActivityId)"
                >
                  {{ $t('groups.viewGroupActivityInfo') }}
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <el-pagination
          v-if="dataList.length"
          style="margin-top: 20px"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @update:page-size="onPageSizeChange"
          @update:current-page="onPageChange"
        />
      </div>
      <!-- 表格主体end -->
    </div>
    <OrderInfo
      v-if="orderInfoVisible"
      ref="orderInfoRef"
    />
    <ActivityInfo
      v-if="activityInfoVisible"
      ref="activityInfoRef"
    />
  </div>
</template>

<script setup>
import OrderInfo from './components/order-info.vue'
import ActivityInfo from './components/activity-info.vue'
import { isAuth } from '@/utils/index'

const dataList = ref([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  activityName: ''
})
let tempSearchForm = null // 保存上次点击查询的请求条件

const activityInfoVisible = ref(false)
const getDataList = (argsPage, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/group/team/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: argsPage == null ? page.currentPage : argsPage.currentPage,
          size: argsPage == null ? page.pageSize : argsPage.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
  })
}
onMounted(() => {
  getDataList()
})
// 查看同团订单
const orderInfoRef = ref(null)
const orderInfoVisible = ref(false)
const viewGroupOrderInfo = id => {
  orderInfoVisible.value = true
  nextTick(() => {
    orderInfoRef.value?.init(id)
  })
}
// 查看活动详情
const activityInfoRef = ref(null)
const viewGroupActivityInfo = id => {
  activityInfoVisible.value = true
  nextTick(() => {
    activityInfoRef.value?.init(id)
  })
}
// 每页数量变更
const onPageSizeChange = val => {
  page.pageSize = val
  getDataList()
}
// 页数变更
const onPageChange = val => {
  page.currentPage = val
  getDataList()
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  page.pageSize = 10
  getDataList(page, newData)
}
const clearSearch = () => {
  searchForm.activityName = null
  searchForm.status = null
}
</script>
<style lang="scss" scoped>
@media (max-width: 1440px) {
  .marginLe {
    margin-left: -5px;
  }
  .marginLBtn {
    margin-left: 0 !important;
  }
}
</style>
